mui.init();
function bindtap(){
	mui("#qucickSelect").on("tap","a",function(e){
		var _coin = this.getAttribute("data-coin");
		var _sendcoin = document.getElementById("sendcoin");
		if(_coin=="max"){
			_sendcoin.value = document.getElementById("userFreeCoin").innerText;
		}else{
			_sendcoin.value = _coin;
		}
	});
	mui("body").on("tap","#sendcoin_okey",function(e){
		var _token = getToKen();
		if(_token['tokey']==''){
			mui.toast('发送红包须要登录');
			return false;
		}
		var _userid = document.getElementById("userid");
		var _sendcoin = document.getElementById("sendcoin");

		if(_userid.value=="" || _userid.value.length <= 0){
			mui.toast("玩家的ID号不能为空");
			_userid.focus();
			return false;
		}
		if(_sendcoin.value=="" || parseFloat(_sendcoin.value) < 10){
			mui.toast("金币数量不能底于10");
			_sendcoin.focus();
			return false;
		}
		var btnArray = ['确定', '取消'];
		mui.confirm('将'+_sendcoin.value+'金币发给编号为'+_userid.value+'的玩家吗？','发红包确认',btnArray,function(e){
			if(e.index==0){
				room_connect(chat.onsend,chat.onmessage,false);
			}
		});
	});
}
var chat = {
	onopen : function(){
	    var _opt = getToKen();
	    _opt['type'] = 'getuserfreecoin',
	    ws.send(JSON.stringify(_opt));
	},
	onsend : function(){
		var _userid = document.getElementById("userid");
		var _sendcoin = document.getElementById("sendcoin");
		_token = getToKen();
	    _token['type'] = 'sendtousercoin';
	    _token['sendcoin'] = parseFloat(_sendcoin.value),
	    _token['sendtouid'] = parseInt(_userid.value),
	    _token['room_id'] = 'public';
	    ws.send(JSON.stringify(_token));
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
		console.log(e.data);
	    switch(data['type']){
	        // 服务端ping客户端
	        case 'ping':
	            ws.send('{"type":"pong"}');
	            break;
	        case 'getuserfreecoin':
	        	switch(data['state']){
	        		case 404:
	        			mui.toast(info[data.msgid]);
	        			break;
	        		case 100:
	        			openLoginWindow('default');
	        			break;
	        		case 200:
	        			document.getElementById("userFreeCoin").innerText = data.coin;
	        			
	        			break;
	        	}
	        	break;
	        case 'sendtousercoin':
	        	switch(data['state']){
	        		case 404:
	        			mui.toast(info[data.msgid]);
	        			break;
	        		case 301:
	        			openLoginWindow('default');
	        			break;
	        		case 200:
	        			document.getElementById("userid").value = '';
						document.getElementById("sendcoin").value = '';
	        			chat.onopen();
	        			mui.alert(info[data.msgid]);
	        			mui.fire(plus.webview.getWebviewById(barItemArray[2]), 'loginSuccess', data);
	            		mui.fire(plus.webview.getWebviewById(barItemArray[4]), 'loginSuccess', data);
	        			break;
	        	}
	        	break;
	    }
	},
}
mui.plusReady(function(e){
	document.addEventListener('loginSuccess',function(e){
		room_connect(chat.onopen,chat.onmessage);
	},false);
	room_connect(chat.onopen,chat.onmessage,false);
	bindtap();
});
